import axios from 'axios'
import { message } from 'antd'
import whiteList from './whiteList.ts'

axios.interceptors.request.use((config: any) => {
    config.headers.Authorization = sessionStorage.getItem('token') ? sessionStorage.getItem('token') : ''
    return config
})


axios.interceptors.response.use((data: any) => {
    // 配置白名单
    if (data.config) {
        const { url, method } = data.config
        let flag = whiteList.some((item: any) => {
            return item.method === data.config.method && data.config.url.includes(item.url)
        })
        // 添加提示消息
        let index = whiteList.findIndex((item: any) => {
            return item.method === data.config.method && data.config.url.includes(item.url)
        })
        // -1 隐式转换为true
        if (index > -1) {
            let msg = whiteList[index].message
            if (flag) {
                message.success(msg? msg: data.data.message);
            }
        }
        return data.data
    } else {
        return data.data
    }
})


export default axios